package com.buyingtickets.controller;

import com.buyingtickets.entity.*;
import com.buyingtickets.service.SiteService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author czl
 * @version 1.0
 * @description: TODO
 * @date 2021/6/25 10:03
 */
@Controller
@RequestMapping("site")
public class SiteController {
    @Resource
    private SiteService siteService;

    @RequestMapping("query")
    public String site(QueryParameter qp,Model model){
        List<Site> siteList=siteService.queryByQuerySite(qp);
         model.addAttribute("siteList",siteList);
         model.addAttribute("qp",qp);
        return "admin_site_query";
    }

    @RequestMapping("{page}/{siteId}/{zhandian}/{sheng}/{shi}/fenye")
    public String fenye(@PathVariable int page,@PathVariable String siteId, @PathVariable String zhandian, @PathVariable String sheng, @PathVariable String shi, Model model){
        System.out.println(page+" "+siteId+" "+zhandian+" "+shi+" "+shi);
        QueryParameter qp=new QueryParameter();
        if (!"!".equals(page)) {
            qp.setPage(page);
        }
        if (!"!".equals(siteId)) {
            qp.setSiteId(siteId);
        }
        if (!"!".equals(zhandian)) {
            qp.setZhandian(zhandian);
        }
        if (!"!".equals(sheng)) {
            qp.setSheng(sheng);
        }
        if (!"!".equals(shi)) {
            qp.setShi(shi);
        }

        return site(qp,model);
    }
    @RequestMapping("site_details/{id}")
    public String site_details(@PathVariable int id,Model model){
        Site site=siteService.queryByID(id);
        model.addAttribute("site",site);
        return "admin_site_details";
    }


    @RequestMapping(value = "update",method = RequestMethod.POST)
    @ResponseBody
    public int update(Site site){
        System.out.println(site);
        return siteService.update(site);
    }

    @RequestMapping( "delete")
    @ResponseBody
    public int delete(int id){
      return siteService.delete(id);
    }
    @RequestMapping("site_add")
    public String add(){
        return "admin_site_add";
    }
    @RequestMapping(value = "add",method = RequestMethod.POST)
    @ResponseBody
    public int add(Site site){
        System.out.println(site);
        return siteService.add(site);
    }
}
